package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * 分类管理
 */
@RestController
@RequestMapping("category")
@Slf4j
public class CategoryController {
    @Resource
    private CategoryService categoryService;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 新增分类
     * @param category 分类信息
     * @return 操作结果
     */
    @CacheEvict(cacheNames = "category",allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody Category category){

        //清除redis缓存
        Set keys = redisTemplate.keys("category*");
        redisTemplate.delete(keys);

        log.info("新增分类信息:{}",category);
        boolean save = categoryService.save(category);
        if (save){
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        //分页构造器
        Page<Category> pageInfo = new Page<>(page,pageSize);
        //条件构造器,通过sort排序
        LambdaQueryWrapper<Category> QueryWrapper = Wrappers.lambdaQuery(Category.class).orderByAsc(Category::getSort);
        //分页查询
        categoryService.page(pageInfo, QueryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 删除分类
     * @param id 要删除的分类id
     * @return 操作结果
     */
    @CacheEvict(cacheNames = "category",allEntries = true)
    @DeleteMapping
    public R<String> deleteById(Long id){
        log.info("删除分类:id为{}",id);
        //清除redis缓存
        Set keys = redisTemplate.keys("category*");
        redisTemplate.delete(keys);

        // 删除分类
        categoryService.remove(id);
        return R.success("删除成功");
    }

    /**
     * 修改分类信息
     * @param category 分类信息
     * @return 操作结果
     */
    @CacheEvict(cacheNames = "category",allEntries = true)
    @PutMapping
    public R<String> updateById(@RequestBody Category category){
        log.info("修改分类信息:{}",category);
        //清除redis缓存
        Set keys = redisTemplate.keys("category*");
        redisTemplate.delete(keys);

        boolean b = categoryService.updateById(category);

        return R.success("修改分类信息成功");
    }

    /**
     * 查询菜品分类
     * @param category 查询条件
     * @return 查询结果
     */
    @GetMapping("/list")
    @Cacheable(cacheNames = "category", key = "''+#category.getType()")
    public R<List<Category>> list( Category category){
        log.info("查询菜品分类");
        //对 key 进行美化,
      /*  String redisKey = "category_list_" + category.getType();*/

        //先去查询redis中的数据
        /*List<Category> categoryList = (List<Category>) redisTemplate.opsForValue().get(redisKey);
        if (categoryList != null ){
            return R.success(categoryList);
        }*/

        LambdaQueryWrapper<Category> queryWrapper = Wrappers.lambdaQuery(Category.class)
                .eq(category.getType() != null, Category::getType, category.getType())
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getUpdateTime);
        //查询菜品分类
        List<Category> list = categoryService.list(queryWrapper);

        //将数据库中 查询的数据存入redis
        /*redisTemplate.opsForValue().set(redisKey, list); // value虽然是List集合,redisTemplate 对value进行序列化*/
        return R.success(list);
    }
}
